package com.typhoon.spring_jdbctemplate.schedule;

import com.typhoon.spring_jdbctemplate.dao.support.PageInfo;
import com.typhoon.spring_jdbctemplate.dao.support.PageQuery;
import com.typhoon.spring_jdbctemplate.dao.support.PageResult;
import com.typhoon.spring_jdbctemplate.entity.Task;
import com.typhoon.spring_jdbctemplate.service.TaskService;
import com.typhoon.spring_jdbctemplate.util.CollectionUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

//@Component
public class AssignSchedule implements InitializingBean {

    @Autowired
    private TaskService taskService;
    /**
     * 线程池
     */
    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3);
    @PostConstruct
    public  void init () {
        this.startAssign();
    }

    public void startAssign() {
        executorService.scheduleWithFixedDelay(new AssignThread(this.taskService),5L,20L,TimeUnit.SECONDS);
    }
    /**
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
    }
}

class AssignThread implements Runnable {

    private TaskService taskService;

    public AssignThread(TaskService taskService) {
        this.taskService = taskService;
    }

    @Override
    public void run() {
        System.out.println("任务分配开始···");
        int total = 0;
        PageQuery pageQuery = new PageQuery(20,1);
        Map<String,Object> params = new HashMap<>();
        params.put("status",2);
        params.put("createTimeEnd",new Date());
        PageResult<Task> pageResult =  this.taskService.page(pageQuery,params);
        if(null == pageResult || CollectionUtils.isEmpty(pageResult.getList())) {
            System.out.println("没有需要分配的任务");
            return;
        }
        PageInfo pageInfo =  pageResult.getPage();
        int totalPage = pageInfo.getTotalPages();
        do {
            pageResult = this.taskService.page(pageQuery,params);
            List<Task> list = pageResult.getList();
            if(CollectionUtils.isEmpty(list)) {
                System.out.println("分配完成");
                break;
            }
            for(Task task : list) {
                this.taskService.doAssign(task);
                total++;
            }
            Long id = list.get(list.size() - 1).getId();
            params.put("idStart",id);
        } while(pageResult.getPage().getCurrentPage() <= pageResult.getPage().getTotalPages());
//        while(true) {
//            pageResult = this.taskService.page(pageQuery,params);
//            List<Task> list = pageResult.getList();
//            if(CollectionUtils.isEmpty(list)) {
//                System.out.println("分配完成");
//                break;
//            }
//            for(Task task : list) {
//                this.taskService.doAssign(task);
//                total++;
//            }
//        }

//        for(int i = 1;i < totalPage + 1; i ++) {
//            pageQuery.setPageIndex(i);
//            pageResult = this.taskService.page(pageQuery,params);
//            List<Task> list = pageResult.getList();
//            for(Task task : list) {
//                this.taskService.doAssign(task);
//                total++;
//            }
//        }
        System.out.println("任务分配结束···,分配了"+total+"条");
    }
}
